package com.ray.test.core;

import com.ray.test.actuator.ActuatorResult;
import com.ray.test.actuator.MethodParams;
import com.ray.test.actuator.spring.ClassSpringActuator;
import com.ray.test.hander.HanderContent;
import com.ray.test.util.ClassUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.runner.RunWith;
import org.springframework.transaction.TransactionStatus;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author bo shen
 * @Description: 类型执行
 * @Class: ClassCore
 * @Package com.ray.test.core
 * @date 2019/12/12 15:31
 * @company <p>杭州传化陆鲸科技有限公司</p>
 * @updateRecord time(修改时间)  author(修改人)   desc(修改内容)
 */
@Slf4j
public class ClassCore implements Core {

    private ClassSpringActuator classSpringActuator = new ClassSpringActuator();

    public CoreResult core(Class<?> clazz, HanderContent handerContent) {
        AtomicInteger success = new AtomicInteger(0);
        AtomicInteger error = new AtomicInteger(0);
        //开始执行
        if (clazz != null && clazz.getAnnotation(RunWith.class) == null) {
            //获取指定排除方法
            List<String> exMethodNames = handerContent.getExcludeMethodName();
            //获取指定执行方法
            List<String> inMethodNames = handerContent.getIncludeMethodName();
            Method[] methods = clazz.getDeclaredMethods();
            log.info("开始执行类:{},一共[{}]个方法", clazz.getName(), methods.length);
            //获取执行对象
            Object target = handerContent.getApplicationContext().getBean(clazz);
            for (Method method : methods) {
                   //只有公有方法才能被执行
                  if(!Modifier.isPublic(method.getModifiers())){
                      continue;
                  }
                  if(CoreUtil.isPass(method.getName(),clazz,inMethodNames,exMethodNames)){
                      continue;
                  }
                //开启事务
                TransactionStatus transactionStatus = handerContent.open();
                try {
                    //构建
                    MethodParams methodParams = new MethodParams();
                    methodParams.setBean(target);
                    methodParams.setTargetClass(clazz);
                    methodParams.setMethod(method);
                    methodParams.setMaps(ClassUtil.getClassType(clazz));
                    ActuatorResult actuatorResult = classSpringActuator.doActuator(methodParams);
                    if (actuatorResult.isResult()) {
                        success.incrementAndGet();
                    } else {
                        error.incrementAndGet();
                    }
                } catch (Exception e) {
                    log.info("单元测试执行异常", e);
                    error.incrementAndGet();
                } finally {
                    //关闭事务
                    handerContent.rollback(transactionStatus);
                }
            }
        }
        return new CoreResult(success.get(), error.get());
    }
}
